package com.yonyou.trumpchi;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yonyou.trumpchi.entity.ServiceInfo;
import com.yonyou.trumpchi.model.ServiceModel;

import java.util.HashMap;

/**
 * 添加服务器
 */
public class AddServiceActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edt_servive_name;
    private EditText edt_servive_url;
    private ImageView img_service_save;
    private Context mContext;
    private TextView tv_add_service_title;
    private Gson gson;
    private ServiceInfo upDataServiceInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_service);
        init();
    }

    private void init() {
        gson = new Gson();
        mContext = this;
        initViews();
    }

    private void initViews() {
        upDataServiceInfo = getIntent().getParcelableExtra("upDataServiceInfo");
        edt_servive_name = (EditText) findViewById(R.id.edt_servive_name);
        edt_servive_url = (EditText) findViewById(R.id.edt_servive_url);
        img_service_save = (ImageView) findViewById(R.id.img_service_save);
        tv_add_service_title = (TextView) findViewById(R.id.tv_add_service_title);
        img_service_save.setOnClickListener(this);
        if (null != upDataServiceInfo) {//修改服务器时
            edt_servive_name.setText(upDataServiceInfo.getName());
            edt_servive_url.setText(upDataServiceInfo.getUrl());
            tv_add_service_title.setText("修改服务器");
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_service_save:
                save();
                break;
        }
    }


    private void save() {
        String name = (edt_servive_name.getText() + "").trim();
        String url = (edt_servive_url.getText() + "").trim();

        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(url)) {
            if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("Https://") || url.startsWith("Http://")) {

                if (null == upDataServiceInfo) {
                    upDataServiceInfo = new ServiceInfo();
                    upDataServiceInfo.setParams(new HashMap<String, String>());
                } else {//修改时删除原来的服务器
                    ServiceModel.del(mContext, upDataServiceInfo.getUrl());
                }
                upDataServiceInfo.setName(name);
                upDataServiceInfo.setUrl(url);
                ServiceModel.add(mContext, upDataServiceInfo);
                finish();
            } else {
                Toast.makeText(mContext, "服务器地址有误", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mContext, "请填写完整", Toast.LENGTH_SHORT).show();
        }
    }

}
